<!doctype html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>言成言成啊</title>
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/meethigher-font.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/animate.css">
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<header class="header">
    <div class="header-menu"><span class="fa fa-bars"></span></div>
    <div class="header-search"><span class="fa fa-search"></span></div>
    <div class="header-title"><a href="/">言成言成啊 | <span class="header-subtitle">Kit Chen&#39;s Blog</span></a></div>
    <ul class="header-navbar clearFix">
        
            <li><a href="/">主页</a></li>
        
            <li><a href="/tags">标签</a></li>
        
            <li><a href="/archives">归档</a></li>
        
            <li><a href="/search">导航</a></li>
        
    </ul>
</header>
<main class="main">
    <div class="main-content meethigher-scroll">
    <article class="post">
        <div class="post-title">
            <h2 class="title">继承案例以及抽象类</h2>
        </div>
        <div class="post-media2">
            <span class="post-time">2019-09-19</span>
            
                <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>
            
        </div>
        <div class="post-content blog-markdown">
            <h1 id="继承案例–发红包"><a href="#继承案例–发红包" class="headerlink" title="继承案例–发红包"></a>继承案例–发红包</h1><h2 id="设计实现"><a href="#设计实现" class="headerlink" title="设计实现"></a>设计实现</h2><img src="/2019/09/19/abstract-and-extends-case/1568901959610.png" title="类图">


<ul>
<li>发红包<ul>
<li>返回值：ArrayList<integer></integer></li>
<li>方法名称：send</li>
<li>参数列表：<ul>
<li>总共发多少钱：int totalMoney</li>
<li>分成多少份：int count</li>
</ul>
</li>
</ul>
</li>
<li>收红包<ul>
<li>返回值：void</li>
<li>方法名称：receive</li>
<li>参数列表：ArrayList<integer> list</integer></li>
</ul>
</li>
</ul>
<h2 id="具体实现"><a href="#具体实现" class="headerlink" title="具体实现"></a>具体实现</h2><ul>
<li>User类</li>
</ul>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">User</span> &#123;</span><br><span class="line">	<span class="keyword">private</span> String name;<span class="comment">// 姓名</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> money;<span class="comment">// 余额，也就是当前用户拥有的钱数</span></span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span>(<span class="params">String name, <span class="keyword">int</span> money</span>)</span> &#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.money = money;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span>(<span class="params">String name</span>)</span> &#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getMoney</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		<span class="keyword">return</span> money;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setMoney</span>(<span class="params"><span class="keyword">int</span> money</span>)</span> &#123;</span><br><span class="line">		<span class="keyword">this</span>.money = money;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 展示一下当前用户有多少钱</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"成员："</span> + name + <span class="string">" 余额："</span> + money);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>Manager类</li>
</ul>
<figure class="highlight axapta"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Manager</span> <span class="keyword">extends</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">	<span class="keyword">public</span> Manager() &#123;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">public</span> Manager(String name, <span class="keyword">int</span> money) &#123;</span><br><span class="line">		<span class="keyword">super</span>(name, money);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">public</span> ArrayList&lt;Integer&gt; send(<span class="keyword">int</span> totalMoney, <span class="keyword">int</span> <span class="keyword">count</span>) &#123;</span><br><span class="line">		<span class="comment">// 首先需要一个集合，用来存储若干个红包的金额</span></span><br><span class="line">		ArrayList&lt;Integer&gt; list = <span class="keyword">new</span> ArrayList&lt;Integer&gt;();</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 首先看一下群主自己有多少钱</span></span><br><span class="line">		<span class="keyword">int</span> leftMoney = <span class="keyword">super</span>.getMoney();<span class="comment">// 群主当前余额</span></span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (totalMoney &gt; leftMoney) &#123;</span><br><span class="line">			System.out.println(<span class="string">"余额不足"</span>);</span><br><span class="line">			<span class="keyword">return</span> list;<span class="comment">// 返回空集合</span></span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 扣钱，重新设置余额</span></span><br><span class="line">		<span class="keyword">super</span>.setMoney(leftMoney - totalMoney);</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 红包需要平均拆分成count份</span></span><br><span class="line">		<span class="keyword">int</span> <span class="keyword">avg</span> = totalMoney / <span class="keyword">count</span>;</span><br><span class="line">		<span class="keyword">int</span> <span class="keyword">mod</span> = totalMoney % <span class="keyword">count</span>;<span class="comment">// 余数，也就是甩下的零头</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">// 除不开的零头，包在最后一个红包当中</span></span><br><span class="line">		<span class="comment">// 下面把红包一个个放到集合当中</span></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">count</span>; i++) &#123;</span><br><span class="line">			<span class="keyword">if</span> (i &gt;= (<span class="keyword">count</span> - <span class="number">1</span>)) &#123;</span><br><span class="line">				list.add(<span class="keyword">avg</span> + <span class="keyword">mod</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			list.add(<span class="keyword">avg</span>);</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> list;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>Member类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Member</span> <span class="keyword">extends</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Member</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>();</span><br><span class="line">		<span class="comment">// TODO Auto-generated constructor stub</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Member</span><span class="params">(String name, <span class="keyword">int</span> money)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>(name, money);</span><br><span class="line">		<span class="comment">// TODO Auto-generated constructor stub</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">receive</span><span class="params">(ArrayList&lt;Integer&gt; list)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// 从所有红包中，随机获取一个</span></span><br><span class="line">		<span class="comment">// 随机获取集合中的一个索引编号</span></span><br><span class="line">		<span class="keyword">int</span> index = <span class="keyword">new</span> Random().nextInt(list.size());</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 根据索引，从集合当中删除，并且得到被删除的红包，给我自己</span></span><br><span class="line">		<span class="keyword">int</span> delta = list.remove(index);</span><br><span class="line">		<span class="comment">// 当前成员本身有多少钱呢？</span></span><br><span class="line">		<span class="keyword">int</span> money = <span class="keyword">super</span>.getMoney();</span><br><span class="line">		<span class="keyword">super</span>.setMoney(money + delta);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>主函数</li>
</ul>
<figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">public <span class="keyword">class</span> MainRedPacker &#123;</span><br><span class="line"></span><br><span class="line">	public static void main(String[] <span class="keyword">args</span>) &#123;</span><br><span class="line">		Manager manager=new Manager(<span class="string">"群主"</span>,1000);</span><br><span class="line">		</span><br><span class="line">		Member <span class="keyword">one</span>=new Member(<span class="string">"成员A"</span>,0);</span><br><span class="line">		Member <span class="keyword">two</span>=new Member(<span class="string">"成员B"</span>,0);</span><br><span class="line">		Member three=new Member(<span class="string">"成员C"</span>,0);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//展示一下个人信息</span></span><br><span class="line">		manager.show();</span><br><span class="line">		<span class="keyword">one</span>.show();</span><br><span class="line">		<span class="keyword">two</span>.show();</span><br><span class="line">		three.show();</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"====================="</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//发红包</span></span><br><span class="line">		ArrayList&lt;Integer&gt; <span class="keyword">list</span>=manager.send(23, 3);</span><br><span class="line">		<span class="comment">//收红包</span></span><br><span class="line">		<span class="keyword">one</span>.receive(<span class="keyword">list</span>);</span><br><span class="line">		<span class="keyword">two</span>.receive(<span class="keyword">list</span>);</span><br><span class="line">		three.receive(<span class="keyword">list</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//再次展示发红包以后各个成员的钱数</span></span><br><span class="line">		manager.show();</span><br><span class="line">		<span class="keyword">one</span>.show();</span><br><span class="line">		<span class="keyword">two</span>.show();</span><br><span class="line">		three.show();</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"====================="</span>);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>运行结果</li>
</ul>
<figure class="highlight asciidoc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">成员：群主 余额：1000</span><br><span class="line">成员：成员A 余额：0</span><br><span class="line">成员：成员B 余额：0</span><br><span class="line">成员：成员C 余额：0</span><br><span class="line">=====================</span><br><span class="line">成员：群主 余额：977</span><br><span class="line">成员：成员A 余额：7</span><br><span class="line">成员：成员B 余额：9</span><br><span class="line">成员：成员C 余额：7</span><br><span class="line">=====================</span><br></pre></td></tr></table></figure>

<h1 id="抽象方法以及抽象类"><a href="#抽象方法以及抽象类" class="headerlink" title="抽象方法以及抽象类"></a>抽象方法以及抽象类</h1><h2 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h2><p>啥是抽象类？直接上图。</p>
 <img src="/2019/09/19/abstract-and-extends-case/1568903209060.png" title="啥是抽象类">

<p><strong>抽象方法：</strong>就是加上abstract关键字，然后去掉大括号，直接分号结束</p>
<p><strong>抽象类：</strong>抽象方法所在的类，必须是抽象类才行。在class之前加上abstract即可</p>
<p><strong>如何使用抽象类和抽象方法：</strong></p>
<ol>
<li>不能直接创建new抽象类对象</li>
<li>必须用一个子类来继承抽象父类</li>
<li>子类<strong>覆盖重写</strong>抽象父类当中<strong>所有的抽象方法</strong><br>覆盖重写：子类去掉抽象方法中的abstract，然后补上方法体大括号</li>
<li>创建子类对象使用</li>
</ol>
<h2 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h2><ul>
<li>Animal类</li>
</ul>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">class</span> <span class="title">Animal</span> &#123;</span><br><span class="line">	<span class="comment">//这是一个抽象方法，代表吃东西，但是具体吃什么(大括号的内容)不确定</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">eat</span>(<span class="params"></span>)</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//这是普通的成员方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">normalMethod</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>Cat类</li>
</ul>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">Cat</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	public void eat() &#123;</span><br><span class="line">		<span class="type">System</span>.out.println(<span class="string">"猫吃鱼"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>主函数</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//Animal animal=new Animal();//错误写法，不能直接创建抽象类</span></span><br><span class="line">		Cat cat=<span class="keyword">new</span> Cat();</span><br><span class="line">		cat.eat();<span class="comment">//输出：猫吃鱼</span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注意事项：</strong></p>
<ol>
<li>一个抽象类不一定含有抽象方法<br>只要保证抽象方法所在的类是抽象类，即可<br>这样没有抽象方法的抽象类，也能直接创建对象，在一些特殊场景下有用途</li>
<li>抽象类的子类必须重写父类中所有的抽象方法，否则，编译会无法通过而报错。除非该子类也是抽象类。</li>
</ol>
        </div>
        
            <div class="busuanzi">
                <span id="busuanzi_container_page_pv">阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            </div>
        
    </article>
</div>
    <pointer href="javascript:void(0);" class="arrow fa fa-arrow-up"></pointer>
<pointer href="javascript:void(0);" class="arrow fa fa-arrow-down"></pointer>

</main>
<aside class="aside">
    <div class="aside-content">
        <div class="aside-avatar"><img src="/images/kitchen.jpg" alt="言成"></div>
        <div class="aside-author">言成</div>
        <div class="aside-description">爱学习，爱运动，爱看妹子</div>
        <ul class="aside-contact clearFix">
            <li><span class="fa fa-github"></span><a href="https://github.com/meethigher">Github</a></li>
            <li><span class="fa fa-envelope"></span><a href="javascript:void(0);">meethigher@qq.com</a></li>
            <li><span class="fa fa-weixin"></span><a href="javascript:void(0);"><img src="/images/weixin.png" alt="扫描微信二维码，加我哦！"></a></li>
        </ul>
    </div>
</aside>
<div class="toolbar"><span class="fa fa-th"></span></div>
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/particles.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/app.js"></script>
<script src="/js/meethigher.js"></script>
<script src="/js/toolbar.js"></script>
</body>
</html>